package com.teng.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.teng.entity.RoleEntity;
import com.teng.util.DBUtil;
import com.teng.util.MD5;

public class RoleDao {
		public void modifypassword(String id,String newPassword)throws Exception{
			Connection connection=DBUtil.getConnection();
			try {
				String sql=" update sys_user set password=?  where id=?";
				PreparedStatement ps=connection.prepareStatement(sql);
				ps.setString(1, MD5.encode(newPassword));
				ps.setString(2, id);
				ps.executeUpdate();
				connection.commit();
			}finally {
				connection.close();
			}
			
		}
		public boolean slectpassword(String id,String oldPassword)throws Exception{
			Connection connection=DBUtil.getConnection();
			try {
				String sql="select * from sys_user where id=?  and password=?";
				PreparedStatement ps=connection.prepareStatement(sql);
				ps.setString(1, id);
				ps.setString(2, oldPassword);
				ResultSet rs=ps.executeQuery();
				boolean a=false;
				if(rs.next()) {
					a=rs.getInt(1)>0;
				}
				return a;
			}finally {
				connection.close();
			}
		}
	
		public void updata(String id, String code, String name, String remark )throws Exception{
			Connection connection=DBUtil.getConnection();
			try {
				String sql=" update sys_role set code=?,name?,remark=?  where id=?";
				PreparedStatement ps=connection.prepareStatement(sql);
				int i=1;
				ps.setString(i++, code);
				ps.setString(i++, name);
				ps.setString(i++, remark);
				ps.setString(i++, id);
				ps.executeUpdate();
				connection.commit();
			}finally {
				connection.close();
			}
		}
		
		
		public boolean seleteId(String id)throws Exception{
			Connection connection=DBUtil.getConnection();
			try {
				String sql=" select count(*) from sys_role_has_menu where role_id=?";
				PreparedStatement ps=connection.prepareStatement(sql);
				ps.setString(1, id);
				ResultSet rs=ps.executeQuery();
				if(rs.next()) {
					return rs.getInt(1)>0;
				}
			}finally {
				connection.close();
			}
			return false;
		}
		
		
		public void del(String id)throws Exception{
			Connection connection=DBUtil.getConnection();
			try {
				String sql=" delete from sys_role where id=?";
				PreparedStatement ps=connection.prepareStatement(sql);
				ps.setString(1, id);
				ps.executeUpdate();
				connection.commit();
			}finally {
				connection.close();
			}
		}
	
		public String checkCreate(String id)throws Exception{
			Connection connection=DBUtil.getConnection();
			try {
				StringBuilder sql=new StringBuilder();
				sql.append(" select name from sys_user ")
					.append(" where id=? ");
				PreparedStatement ps=connection.prepareStatement(sql.toString());
				ps.setString(1, id);
				ResultSet rs=ps.executeQuery();
				String re = null;
				if(rs.next()) {
					re=rs.getString("name");
				}
				return re;
			}finally {
				connection.close();
			}
		}
	
		public RoleEntity check(String id)throws Exception{
			Connection connection=DBUtil.getConnection();
			try {
				StringBuilder sql=new StringBuilder();
				sql.append(" select * from sys_role ")
					.append(" where id=? ");
				PreparedStatement ps=connection.prepareStatement(sql.toString());
				ps.setString(1, id);
				ResultSet rs=ps.executeQuery();
				RoleEntity re=new RoleEntity();
				if(rs.next()) {
					re=deposit(rs);
				}
				return re;
			}finally {
				connection.close();
			}
			
			
		}
	
		public List<RoleEntity> list(String id)throws Exception{
			
			Connection connection=DBUtil.getConnection();
			try {
				String sql="select * from sys_role where create_user_id=?";
				PreparedStatement ps=connection.prepareStatement(sql);
				ps.setString(1, id);
				ResultSet rs=ps.executeQuery();
				List<RoleEntity> re=new ArrayList<>();
				while(rs.next()) {
					re.add(deposit(rs));
				}
				
				return  re;
			}finally {
				connection.close();
			}
			
		}
	
	
		public boolean validatecode(String code)throws Exception {
			Connection connection=DBUtil.getConnection();
			try {
			String sql="select count(*) from sys_role where code=?";
			PreparedStatement ps=connection.prepareStatement(sql);
			ps.setString(1, code);
			ResultSet rs=ps.executeQuery();
			if(rs.next()) {
				return rs.getInt(1)>0;
			}
			return false;
			}finally {
				connection.close();
			}
		}
		
		public void insert(RoleEntity re)throws Exception {
			Connection connection=DBUtil.getConnection();
			try {
				StringBuilder sql=new StringBuilder();
				sql.append("insert into sys_role ")
					.append(" (id,code,name,remark,create_user_id,create_time)")
					.append(" values ")
					.append(" (?,?,?,?,?,?) ");
				PreparedStatement ps=connection.prepareStatement(sql.toString());
				int i=1;
				ps.setString(i++, re.getId());
				ps.setString(i++, re.getCode());
				ps.setString(i++, re.getName());
				ps.setString(i++, re.getRemark());
				ps.setString(i++, re.getCreateUserId());
				ps.setTimestamp(i++, toTimestamp(re.getCreateTime()));
				ps.executeUpdate();
				connection.commit();
			}finally {
				connection.close();
			}
		}
		
		public static Timestamp toTimestamp(java.util.Date d ) {
			if (d != null) {
				return  new Timestamp(d.getTime());
			} 
			return null;
		}
		
		public RoleEntity deposit(ResultSet rs) throws Exception{
			RoleEntity re=new RoleEntity();
			re.setCode(rs.getString("code"));
			re.setCreateTime(rs.getDate("create_time"));
			re.setCreateUserId(rs.getString("create_user_id"));
			re.setId(rs.getString("id"));
			re.setName(rs.getString("name"));
			re.setRemark(rs.getString("remark"));
			re.setCreateName(checkCreate(rs.getString("create_user_id")));
			return re;
			
		} 
}
